﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CuaHangMayTinh.BusinessEntities;
using CuaHangMayTinh.Utilities;
using CuaHangMayTinh.DataProvider;
using System.Data;
using System.Data.SqlClient;
using Microsoft.SqlServer.Management.Common;
using Microsoft.SqlServer.Management.Smo;

namespace CuaHangMayTinh.DataAccessLayer
{
    /// <summary>
    /// BackupRestoreDA: Backup và Restore Database
    /// </summary>
    public class BackupRestoreDA
    {
        public BackupRestoreDA()
        {

        }

        /// <summary>
        /// Hàm dùng để backup CSDL
        /// </summary>
        /// <param name="strFileName">tên của file .bak muốn lưu lại</param>
        /// <returns>true: Thành Công</returns>
        /// <returns>false: Thất Bại</returns>
        public bool BackUp(string strFileName)
        {
            
            try
            {
                ParameterBuilder pb = DBFactory.CreateParamBuilder();

                pb.AddParameter("strFileName", strFileName);

                return DBFactory.Database.ExecuteNonQueryGetID("Backup_Data", pb.Parameters) == 1;

                //string command = @"BACKUP DATABASE CHMT TO DISK='" + strFileName + "'";
                //return DBFactory.Database.ExecuteScalar(command) == null;
            }
            catch (Exception ex)
            {
                Logger.Write(ex.Message);
                throw new Exception(ex.Message);
            }
        }

        /// <summary>
        /// Hàm Restore CSDL
        /// </summary>
        /// <param name="strFileName">tên file muốn restore</param>
        /// <param name="user">tên đăng nhập CSDL</param>
        /// <param name="pass">password</param>
        /// <returns>true: Thành Công</returns>
        /// <returns>false: Thất Bại</returns>
        public bool Restore(string strFileName, string user, string pass)
        {
            try
            {
                ServerConnection conn = new ServerConnection(@"dandabook.no-ip.biz,1433\SQLEXPRESS", user, pass);

                Server localServer = new Server(conn); //local using windows athuentication

                Restore backupMgr = new Restore();
                backupMgr.Devices.AddDevice(@"C:\Backup\"+strFileName+".bak", DeviceType.File);
                backupMgr.Database = "CuaHangMayTinh";
                backupMgr.Action = RestoreActionType.Database;
                backupMgr.ReplaceDatabase = true;
                backupMgr.NoRecovery = false;
                backupMgr.SqlRestore(localServer);
                return true;
            }
            catch (SqlException ex)
            {
                Logger.Write(ex.Message);
                throw new Exception(ex.Message);
            }
        }

        /// <summary>
        /// Hàm kiểm tra trùng tên file backup
        /// </summary>
        /// <param name="strFileName"></param>
        /// <param name="BK_RS"></param>
        /// <returns>True: đã có</returns>
        /// <returns>False: chưa có</returns>
        public bool CheckName(string strFileName, bool BK_RS)
        {
            try
            {
                ParameterBuilder pb = DBFactory.CreateParamBuilder();

                pb.AddParameter("Name", strFileName);
                pb.AddParameter("BK_RS", BK_RS);

                return DBFactory.Database.ExecuteNonQueryGetID("LogBackupRestore_CheckName", pb.Parameters) == 1;
            }
            catch (Exception ex)
            {
                Logger.Write(ex.Message);
                throw new Exception(ex.Message);
            }
        }
    }
}
